//
//  LDCacheManager.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/4.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
import YYCache
import SDWebImage
//import Realm

class LDCacheManager: NSObject {
    
    internal var userCacheManger:YYCache?
    internal var sdImageManger:SDWebImageManager!
    //入口
    class var shareInstance:LDCacheManager {
        struct Staics {
            static let instance = LDCacheManager()
        }
        return Staics.instance
    }
    
    
    
    
    
    //
    override init() {
        
//        if DDClientInfoManager.clientType() == DDClientType.Boss {
        
            userCacheManger = YYCache.init(path: LDCache.JobFinderInfoString)
            sdImageManger = SDWebImageManager()
            
//        } else {
//            
//            userCacheManger = YYCache.init(path: LDCache.EmployerInfoString)
//        }
        
    }
    
    
    //clean
    
    func cleanAllCache() {
        
        userCacheManger?.removeAllObjects()
    }
    
    //读取
    func readFromCache(key:String, colsure:(key:String,value:NSCoding?)->()) {
        self.userCacheManger?.objectForKey(key, withBlock: { (string, data) in
            
               colsure(key: string,value: data)
        })
    }
    
    //缓存
    func saveToCache(key:String,value:NSCoding,success:(()->())?) {
        
        userCacheManger?.removeObjectForKey(key, withBlock: { [weak self] (name) in
//            debugPrint("删除\(name)成功")
            self?.userCacheManger?.setObject(value, forKey: key, withBlock: {
//                debugPrint("缓存\(name)成功")
                
                if success != nil {
                    success!()
                }
                
            })
            })
    }
    
}
